﻿using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyWeb.ServiceBus.Extensions.Consul;
using MyWeb.ServiceBus.Extensions.Middleware;
using MyWeb.ServiceBus.Extensions.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Extensions
{
    /// <summary>
    /// 项目中间件
    /// </summary>
    public static class ProgramMiddleware
    {
        public static void UseProgramMiddleware(this IApplicationBuilder app, IServiceCollection services, IConfigurationRoot? config, string address)
        {
            if (app == null) throw new ArgumentNullException(nameof(app));

            app.UseMiddleware<HttpContextMiddleware>();

            app.UseSwaggerMiddle();
            // 允许所有跨域，CorsPolocy是在ConfigureServices方法中配置的跨域策略名称
            app.UseCors("CorsPolocy");

            var isConsul = config.GetValue<bool>("Consul:enable");
            if (isConsul) { app.UseConsul(services, config, address); }
        }
    }
}
